package com.sts.controller;

import com.sts.common.result.Result;
import com.sts.dto.AttachmentDTO;
import com.sts.dto.AttachmentUploadDTO;
import com.sts.service.AttachmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/attachment")
@RequiredArgsConstructor
public class AttachmentController {

    private final AttachmentService attachmentService;

    /** 文件上传，支持 ticket 或 comment */
    @PostMapping("/upload")
    public Result<AttachmentDTO> upload(
            @RequestPart("file") MultipartFile file,
            @RequestParam(required = false) Long ticketId,
            @RequestParam(required = false) Long commentId,
            @RequestHeader("X-User-Id") Long uploaderId) {

        AttachmentUploadDTO dto = new AttachmentUploadDTO();
        dto.setFile(file);
        dto.setTicketId(ticketId);
        dto.setCommentId(commentId);

        return Result.success(attachmentService.upload(dto, uploaderId));
    }

    @GetMapping("/ticket/{tId}")
    public Result<List<AttachmentDTO>> listByTicket(@PathVariable Long ticketId) {
        return Result.success(attachmentService.listByTicket(ticketId));
    }

    @GetMapping("/comment/{cId}")
    public Result<List<AttachmentDTO>> listByComment(@PathVariable Long commentId) {
        return Result.success(attachmentService.listByComment(commentId));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id,
                               @RequestHeader("X-User-Id") Long operatorId) {
        attachmentService.delete(id, operatorId);
        return Result.success();
    }
}